var express = require('express');
var router = express.Router();
var mongo = require("./db");
var mongodb = require("mongodb");
var ObjectId = mongodb.ObjectId;
/* GET users listing. */
router.get('/', function(req, res, next) {
    res.send('respond with a resource');
});
// 添加留言接口
router.post("/add", function(req, res) {
        var obj = {
            nickname: req.body.nickname,
            msg: req.body.msg,
            time: new Date().getTime()
        }
        mongo("add", "msglist", obj, function() {
            res.send({ code: 0, success: "添加成功" })
        })
    })
    // 查询接口
router.get("/list", function(req, res) {
        mongo("find", "msglist", {}, function(data) {
            if (data.length == 0) {
                res.send({ code: 700, err: "暂无数据", data: [] })
            } else {
                res.send({ code: 0, success: "获取列表成功", data: data })
            }
        })
    })
    // 删除接口
router.post("/del", function(req, res) {
    mongo("del", "msglist", { _id: ObjectId(req.body.id) }, function() {
        res.send({ code: 0, success: "删除成功" })
    })
})
module.exports = router;